package com.yjd.comm.base.control;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.message.service.IVerifySmsService;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.enums.ClientChannelEnum;
import com.yjd.comm.user.util.UserUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.vo.CachedKeys;

/**
 * <b>description</b>：移动端control的基类 <br>
 * <b>time</b>：2015-1-20下午4:32:03 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class MobileBaseControl extends BaseControl {

	@Override
	protected UserSession getUserSession() {
		return UserUtil.getUserModel(request, response,
				ClientChannelEnum.MOBILE);
	}
	
	/**
	 * 比较验证码
	 * 
	 * @param mobile
	 * @param code
	 * @return boolean
	 * @throws Exception
	 */
	protected boolean compareMessageCode(String npNumber, String msgCode)
			throws Exception {
		if (StringUtils.isBlank(npNumber) || StringUtils.isBlank(msgCode)
				|| !NumberUtils.isNumber(npNumber)) {
			FrameUtil.throwBaseException1("参数异常");
		}
		
		int msgCount = 1;
		// 验证码错误次数
		Object o = ServletUtil.getSession(request, response,
				CachedKeys.MSG_CODE_COUNT);
		if (o != null) {
			msgCount = (int) o;
		}
		
		int result = verifySmsService.compareMessageCode(npNumber, msgCode, msgCount, 3);
		
		//校验通过
		if(result == 0){
			return true;
		}
		
		//无效验证码
		if(result == -1000){
			return false;
		}
		
		//验证码达到最大错误次数
		if(result == -1001){
			ServletUtil.removeSession(request, response, CachedKeys.MSG_CODE_COUNT);
			return false;
		}
		
		//验证码不匹配
		if(result == -1002){
			ServletUtil.putSession(request, response, CachedKeys.MSG_CODE_COUNT, msgCount + 1);
			return false;
		}
		
		return false;
	}
	
	@ServiceRemoteProxy
	protected IVerifySmsService verifySmsService;
}
